package com.zbkj.crmeb.task.product;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zbkj.crmeb.store.model.StoreBrand;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.service.StoreBrandService;
import com.zbkj.crmeb.store.service.StoreProductService;

import lombok.extern.slf4j.Slf4j;

/**
 * x
 * 品牌商品数量统计服务
 * @author muyu.myw
 * @date 2020/9/27 16:29
 */
@Slf4j
@Configuration
public class BrandTotalTask {
    @Autowired
    StoreBrandService storeBrandService;
    @Autowired
    private StoreProductService storeProductService;

    /**
     * 统计所有品牌的商品数量，更新品牌表30分钟同步一次数据
     */
	@XxlJob("vrandTotalTask")
//	@PostConstruct
    public void runTask() {
        try {
            log.info("run BrandTotalTask!");
            List<Map<String, Object>> list = storeProductService.listMaps(Wrappers.<StoreProduct>query()
                    .select("brand_id brandId", "count(*) count")
                    .eq("is_del", 0)
                    .eq("is_show", 1)
                    .gt("stock", 0)
                    .notIn("is_europe", 1)
                    .ne("brand_id", 0)
                    .groupBy("brand_id")
                    );
            storeBrandService.update(Wrappers.<StoreBrand>lambdaUpdate().set(StoreBrand :: getProductCount, 0));
            storeBrandService.updateList(list);
        } catch (Exception e) {
            log.error("BrandTotalTask.task error ! e :", e);
        }

    }
}
